package com.camtc.camcenter.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger 配置
 *
 * @author
 * @date
 */

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket  petApi() {
        // 添加请求参数，将token作为请求头部传入后端
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameters = new ArrayList<>();
        // 添加 JWT令牌作为验证
        parameterBuilder.name("Authorization")
                        .description("jwt令牌")
                        .modelRef(new ModelRef("string"))
                        .parameterType("header")
                        .required(false).build();
        parameters.add(parameterBuilder.build());
        return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.camtc.camcenter.controller"))
                    .paths(PathSelectors.any())
                    .build()
                    .globalOperationParameters(parameters);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .version("1.0")
                .title("cam_center")
                .description("cam 开发框架服务API")
                .build();
    }
}
